package ranab.server.ftp.usermanager;

import java.io.File;
import java.io.IOException;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Collection;
import java.util.Collections;
import java.util.Enumeration;
import java.util.ArrayList;
import java.util.Iterator;

import ranab.io.IoUtils;
import ranab.io.LogFile;
import ranab.util.BaseProperties;
import ranab.util.EncryptUtils;
import ranab.server.ftp.FtpConfig;

import com.webdeninteractive.bie.persistent.privileges.*;

/**
 * Properties file based <code>UserManager</code>
 * implementation. We use <code>user.properties</code> file
 * to store user data.
 * 
 * @author <a href="mailto:rana_b@yahoo.com">Rana Bhattacharyya</a>
 */
public class BIEUserManager extends UserManager  {

    private final static String PREFIX    = "FtpServer.user.";
    private final static String USER_PROP = "user.properties";

    private BaseProperties mUserData;
    private File       mUserDataFile;
    private boolean    mbEncrypt;
    private FtpConfig cfg;
    
    private long mlLastModified;
    
    /**
     * Instantiate user manager.
     *
     * @param cfg Ftp config object.
     */
    public BIEUserManager(FtpConfig cfg) throws Exception {
        super(cfg);
        this.cfg =cfg;        
    }


    /**
     * Save user data. Store the properties.
     */
    public void save(User usr) throws IOException {
    }
     

    /**
     * Delete an user. Removes all this user entries from the properties.
     * After removing the corresponding from the properties, save the data.
     */
    public void delete(String usrName) throws IOException {
     }
    
    
    /**
     * Get user password. Returns the encrypted value.
     * If the password value is not null
     *    password = new password 
     * else 
     *   if user does exist
     *     password = old password
     *   else 
     *     password = ""
     */
    private String getPassword(User usr) {
        return "sldklsdfsld";
    } 
    
    
    /**
     * Get all user names.
     */
    public Collection getAllUserNames() {
        return null;
    }


    /**
     * Load user data.
     */
    public User getUserByName(String userName) {		                                  
        User user = new User();
        user.setName(userName);
        user.setEnabled(true);        
        user.getVirtualDirectory().setRootDirectory(cfg.getDefaultRoot());
        user.getVirtualDirectory().setWritePermission(true);
        user.setMaxIdleTime(cfg.getDefaultIdleTime());
        user.setMaxUploadRate(0);
        user.setMaxDownloadRate(0);
        return user;
    }
	    
    /**
     * User existance check
     */
    public boolean doesExist(String name) {
        return true;
    }
    
    
    /**
     * User authenticate method
     */
    public boolean authenticate(String user, String password) {
        try {            
            if (Privileges.authenticate(user,password)) {
                return true;
            }else {
                return false;
            }        
                
        }catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
    
    /**
     * Reload the user data if necessary
     */
    public void reload() throws Exception {
    }
    
    /**
     * Close the user manager - remove existing entries.
     */
    public void dispose() {
        if (mUserData != null) {
            mUserData.clear();
            mUserData = null;
        }
    }
}

